<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.UUID" %>
<%@ page import="cn.myschool.entity.Student" %>
<%@ page import="cn.myschool.util.StrUtil" %><%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2022/7/7
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //临时存放的目录
    DiskFileItemFactory d=new DiskFileItemFactory();
    ServletFileUpload sp=new ServletFileUpload(d);
    sp.setHeaderEncoding("utf-8");
    List<FileItem> fileItems = sp.parseRequest(request);
    Student stu=new Student();
    for(FileItem f:fileItems){
        out.print("<p>");
        if(f.isFormField()){
            out.print("这是一个表单域：名字是："+f.getFieldName()+",值是"+f.getString("utf-8"));
            switch (f.getFieldName()){
                case "sname":
                    stu.setSname(f.getString("utf-8"));
                    break;
                case "sex":
                    stu.setSex(f.getString("utf-8"));
                    break;
                case "gradeid":
                    stu.setGradeid(StrUtil.toInt(f.getString("utf-8")));
                    break;
                case "enterdate":
                    stu.setEnterdate(StrUtil.toDate(f.getString("utf-8")));
                    break;
            }
        }else{
            out.print("这是一个文件域，名字是"+f.getFieldName());
            if(f.getSize()<1)
                continue;
            out.print("文件名是："+f.getName());
            String fileName=f.getName();
            String houzhui=fileName.substring(fileName.lastIndexOf('.'));
            String newName= UUID.randomUUID()+houzhui;
            File newPic=new File(application.getRealPath("images"));
            if(newPic.exists()==false)
                newPic.mkdirs();

            f.write(new File(newPic,newName));
            stu.setIdcardpic("images/"+newName);///!!!!!!!
        }
        out.print("</p>");
    }
    //save(student)
%>
<img src="<%=stu.getIdcardpic()%>"/>
  <%--<%--%>
      <%--request.setCharacterEncoding("utf-8");--%>
      <%--InputStream inputStream = request.getInputStream();--%>
      <%--int ch=0;--%>
      <%--while ((ch=inputStream.read())!=-1){--%>
          <%--out.print((char)ch);--%>
      <%--}--%>

<%--//      String name=request.getParameter("sname");--%>
<%--//      String phone=request.getParameter("telephone");--%>
<%--//      String idcard=request.getParameter("idcardpic");--%>
  <%--%>--%>
<%--<%=name%>
<%=phone%>
<%=idcard%>--%>
</body>
</html>
